package cn.net.shuxing.yifu.common.service;


import cn.net.shuxing.yifu.common.bean.ShareholderBonus;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserRealInfo;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.mapper.ShareholderBonusMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import cn.net.shuxing.yifu.common.util.LocalDateUtil;
import cn.net.shuxing.yifu.common.util.TimeExample;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Service
public class ShareholderBonusService extends BaseService<ShareholderBonus, ShareholderBonusMapper> {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRealInfoService userRealInfoService;

    public List<ShareholderBonus> getsByShareholderId(Integer shareholderId) {
        CommonExample example = new CommonExample(ShareholderBonus.class);
        example.equals("shareholderId", shareholderId);
        List<ShareholderBonus> shareholderBonuses = mapper.selectByExample(example);
        completeAll(shareholderBonuses);
        return shareholderBonuses;
    }

    public List<ShareholderBonus> getsDisByShareholderId(Integer shareholderId) {
        CommonExample example = new CommonExample(ShareholderBonus.class);
        example.equals("shareholderId", shareholderId);
        example.equals("bonus", new BigDecimal(0));
        example.setOrderByClause("bonus ASC");
        return getsByExample(example);
    }

    public List<ShareholderBonus> getThisMonthBonusByShareholderId(Integer shareholderId) {
        TimeExample example = new TimeExample(ShareholderBonus.class);
        example.equals("shareholderId",shareholderId);
        LocalDate thisMonthFirstDay = LocalDateUtil.getThisMonthFirstDay();
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createDate", thisMonthFirstDay);
        return getsByExample(example);
    }

    public BigDecimal getThisMonthBonus() {
        List<User> userList = userService.getsByUserType(UserType.SHAREHOLDER);
        BigDecimal monthDividends = new BigDecimal(0);
        for (User user : userList) {
            List<ShareholderBonus> thisMonthBonusByShareholderId = getThisMonthBonusByShareholderId(user.getId());
            for (ShareholderBonus shareholderBonus : thisMonthBonusByShareholderId) {
                BigDecimal bonus = shareholderBonus.getBonus();
                monthDividends = monthDividends.add(bonus);
            }
        }
        return monthDividends;
    }

    @Override
    protected void complete(ShareholderBonus bean) {
        if (bean == null)
            return;
        Integer shareholderId = bean.getShareholderId();
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(shareholderId);
        String realName = "";
        if (userRealInfo != null)
          realName = userRealInfo.getRealName();

        bean.setShareholderName(realName);
    }

    public List<ShareholderBonus> getBonusByCreateDate(LocalDate createDate) {
        TimeExample example = new TimeExample(ShareholderBonus.class);
        example.equals("createDate", createDate);
        return getsByExample(example);
    }

}
